package co.edu.unal.bioingenium.kbmed.knowledge.weirdness;

import co.edu.unal.bioingenium.kbmed.config.Configuration;
import gnu.trove.map.TObjectFloatMap;
import gnu.trove.map.hash.TObjectFloatHashMap;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Ing. Alejandro Riveros Cruz
 */
public class WeirdnessService {

    private static WeirdnessService weirdnessServiceInstance;
    private TObjectFloatMap<String> weirdnessMap;

    public static WeirdnessService getInstance() {
        if (weirdnessServiceInstance == null) {
            weirdnessServiceInstance = new WeirdnessService();
        }
        return weirdnessServiceInstance;
    }

    private WeirdnessService() {
        loadWeirdness("RAE-20000words", "conceptAllTerms");
    }

    /**
     * 
     * @param RAEDictionaryName
     * @param corpusName
     */
    private WeirdnessService(String RAEDictionaryName, String corpusName) {

        loadWeirdness(RAEDictionaryName, corpusName);
    }

    /**
     * 
     * @param conceptId
     * @return
     */
    public float getWeirdness(String conceptId) {
        return weirdnessMap.get(conceptId);
    }

    /**
     * 
     * @param RAEDictionaryName
     * @param corpusName
     */
    private void loadWeirdness(String RAEDictionaryName, String corpusName) {
        try {
            weirdnessMap = new TObjectFloatHashMap<String>();
            BufferedReader bufferedReader = new BufferedReader(new FileReader(new File(Configuration.DATA_DIR + File.separator + corpusName + "-" + RAEDictionaryName + "ConceptWeirdness.csv")));
            String line;
            String[] tokens;
            while ((line = bufferedReader.readLine()) != null) {
                tokens = line.split(",");
                weirdnessMap.put(tokens[0], Float.parseFloat(tokens[1]));
            }
            bufferedReader.close();
        } catch (IOException ex) {
            Logger.getLogger(WeirdnessService.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
